'''
#=============================================================================
#     FileName: views.py
#         Desc: 
#       Author: ruoshui
#      Version: 0.0.1

#      History:
#=============================================================================
'''
# coding=utf-8
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from contextlib import closing
from pi_car import app
import re
import RPi.GPIO as GPIO
import time
import signal
import atexit
import PowerMotor

# atexit.register(GPIO.cleanup)


@app.route('/')
def show_index():
    return render_template('home.html')


@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "GET":
        return "get" + request.form["user"]
    elif request.method == "POST":
        return "post"





@app.route('/ctl', methods=['GET', 'POST'])
def ctrl_id():
    if request.method == 'POST':
        id = request.form['id']
        print id
        PowerMotor.init()
        if id == 't_left':
            PowerMotor.t_left()
            return "left"
        elif id == 't_right':
            PowerMotor.t_right()
            return "right"
        elif id == 't_up':
            PowerMotor.t_up()
            return "up"
        elif id == 't_down':
            PowerMotor.t_down()
            return "down"
        elif id == 't_stop':
            PowerMotor.t_stop()
            return "stop"
        elif id == 't_servod':
            # t_servod()
            return "servod"
        elif id == 't_open':
            # t_open()
            return "open"
        elif id == 't_close':
            # t_close()
            return "close"
    return redirect(url_for('show_index'))



